package cn.bytecollege.admin.controller;

import cn.bytecollege.admin.service.AdminService;
import cn.bytecollege.common.constant.Resource;
import cn.bytecollege.common.entity.Admin;
import cn.bytecollege.common.util.MD5Util;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "AdminRegisterController", value = "/admin/adminRegister")
public class AdminRegisterController extends HttpServlet {
    AdminService adminService = null;
    public AdminRegisterController() {
        adminService = new AdminService();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher(Resource.PAGE_LOCATION+"/admin/reg.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String name = request.getParameter("name");

        Admin admin = new Admin();
        admin.setAdminUsername(username);
        admin.setAdminPassword(MD5Util.md5Encode(password));
        admin.setAdminName(name);
        admin.setTurnover(0.0);

        adminService.saveAdmin(admin);

        String msg = null;
        msg = "注册成功，请登录";
        request.setAttribute("msg",msg);
        request.getRequestDispatcher(Resource.PAGE_LOCATION+"/admin/login.jsp").forward(request,response);
    }
}
